package com.example.mydemo103.DB;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.mydemo103.bean.Book;
import com.example.mydemo103.bean.User;

import java.text.SimpleDateFormat;
import java.util.Date;

public class UserService {
    private  MyDBHekper myDBHelper;
    private SQLiteDatabase db;


    public UserService() {
    }

    public  UserService(Context context){
        if(db==null){
            myDBHelper=new MyDBHekper(context);//getApplicationContext()
            db=myDBHelper.getWritableDatabase();//打开数据库
        }

    }

    /**
     * 关闭数据库
     */
    public void closeDB(){
        if(db!=null){
            db.close();
        }
    }
    public Book borrowbook(int bookimg){
        Book book=new Book();
            book.setBookimg(bookimg);
        return book;
    }
    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    public Object checkLogin(String username, String password) {

        Cursor cursor = db.query("tbl_user", new String[]{}, "username=? and password=?", new String[]{username, password},
                null, null, null);
        User user = null;

        if (cursor.moveToNext()) {
            user = new User();
//            user.setUid(cursor.getInt(0));
            user.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            user.setPassword(cursor.getString(cursor.getColumnIndex("password")));
//            user.setSex(cursor.getString(cursor.getColumnIndex("sex")));
//            user.setTelphone(cursor.getInt(cursor.getColumnIndex("telephone")));
//            user.setRole(cursor.getInt(cursor.getColumnIndex("role")));
//            user.setBalance(cursor.getDouble(cursor.getColumnIndex("balance")));
//            user.setRegtime(cursor.getString(cursor.getColumnIndex("regTime")));
//            user.setTurname(cursor.getString(cursor.getColumnIndex("name")));
        }

            return user;
        }
        //根据uid来查询用户  findUserById
        //根据username来查询用户  findUserByName

        /**
         * 注册
         * 当前时间，用户身份为1
         * @param username
         * @param password
         * @return
         */
        public int insertUser (String  username, String  password,  String  phone){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            String mydate = df.format(new Date());
            ContentValues cv = new ContentValues();
            cv.put("username", username);
            cv.put("password", password);
            cv.put("regTime", mydate);
            cv.put("telephone", phone);
            cv.put("role", 1);
            int i = (int) db.insert("tbl_user", null, cv);
            return i;
        }
//
        public void updateName (String name ,String newname) {
            ContentValues newValues = new ContentValues();
            newValues.put("username", newname);
            Log.e("控制层用户名", newname);
             db.update("tbl_user", newValues, "username=?", new String[]{name});
        }
    }
